<!-- 报告报批 -->
<template>
  <div>
    <card>
      <p slot="title">报告报批</p>
      <table id="reportApproval" width="100%" cellspacing="0" cellpadding="0" border="1" ref="print">
        <tr>
          <th colspan="4">
            报告报批表
          </th>
        </tr>
        <tr>
          <td width="25%">项目编号</td>
          <td colspan="3" class="sizeStyle">
            {{info.ProjectNumber}}
          </td>
        </tr>
        <tr>
          <td>项目名称</td>
          <td colspan="3" class="sizeStyle">
            {{info.ProjectName}}
            <Icon type="ios-arrow-down" size='15' style="cursor:pointer" @click="viewClick($event,'projectName')" v-if='projectNameList.length>0'/>
            <Oldname v-if="projectName" :oldName="projectNameList"></Oldname>
            <!-- <Input v-model="info.Project_name" /> -->
          </td>
        </tr>
        <tr>
          <td>建设单位</td>
          <td colspan="3">
            <Input v-model="info.BuildUnit" :disabled='viewOnly'/>
            <Icon type="ios-arrow-down" size='15' style="cursor:pointer" @click="viewClick($event,'buildName')" v-if='buildNameList.length>0'/>
            <Oldname v-if="buildName" :oldName="buildNameList"></Oldname>
          </td>
        </tr>
        <tr>
          <td>建设地点</td>
          <td colspan="3" class="sizeStyle">
            {{info.BuildAddress}}
            <!-- <Input v-model="info.Build_address" /> -->
          </td>
        </tr>
        <tr>
          <td>报告类型</td>
          <td colspan="3" class="sizeStyle">
            {{info.ReportType}}
            <!-- <Input v-model="info.Report_type" /> -->
          </td>
        </tr>
        <tr>
          <td>项目负责人</td>
          <td width="25%" class="sizeStyle">
            {{info.PerjectLeader}}
          </td>
          <td width="25%">项目组其他成员</td>
          <td class="sizeStyle">
            {{cyz}}
          </td>
        </tr>
        <tr>
          <td>审批部门</td>
          <td class="sizeStyle">{{info.ApprovalUnit}}</td>
          <td>报批时间</td>
          <td>
            <Input v-model="info.BatchDate" type="text" :isDisabled='viewOnly'/>
          </td>
        </tr>
        <tr>
          <td>报批版报告</td>
          <td colspan="3">
            <Col span="12"><UploadFile v-model="info.ReportBatch" :upName="ReportBatch" UploadFileName="ReportBatch" :UploadFileId="$route.query.ProjectId" :isDisabled='viewOnly'/></Col>
            <Col span="12"><DownloadButton v-if="viewOnly" :downData="downloadInfo.ReportBatch"></DownloadButton></Col>
            <p style="font-size:12px;color:#666;line-height:32px;">上传文件格式为docx、doc、PDF、图片、压缩文件</p>
          </td>
        </tr>
      </table>
      <Row type="flex" justify="space-between" class="topmargin">
        <Col span="12">
        <PrintButton v-if="flag" Url="print/printBatch" :Project_id="info.Id"></PrintButton>
        </Col>
        <Col span="12">
        <div style="float: right">
          <Button type="primary" @click="submit('save')" :disabled='viewOnly'>保存</Button>
          <Button type="primary" @click="submit('submit')" :disabled='viewOnly'>提交公示</Button>
        </div>
        </Col>
      </Row>
      <StepBar :status="7" :viewOnly='viewOnly'/>
    </card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 项目名称、建设单位、法人、联系人、联系电话留痕列表
        projectName: false,
        buildName: false,
        projectNameList: [], 
        buildNameList: [], 
        viewOnly: false,  // 允许查看
        flag: false,
        info: {
          info: {}
        },
        cyz: '',
        ReportBatch: "",
        downloadInfo: {},
        updatedInfo: 0,
        setButton: 0,
      }
    },
    created() {
      // let query = this.$route.query
      // this.query = query
      // this.getProject(this.query.id)
      //判断是否有ID
      if (this.$route.query.ProjectId && this.$route.query.ProjectId != "") {
        let query = this.$route.query
        this.query = query
        this.getProject(this.$route.query.ProjectId);
        if(this.$route.query.viewOnly==1 || localStorage.getItem('userOrg') ==7) {
          this.viewOnly = true
        }
      } else {
        //跳转首页
        this.$router.push({
          name: "index"
        });
      }
      // 项目成员组
      this.findStaffs()
    },
    methods: {
      // 名字去重显示
      viewClick(e,nameShow) {
        if(!this[nameShow]) {
          e.target.className = 'ivu-icon ivu-icon-ios-arrow-up'
        }else {
          e.target.className = 'ivu-icon ivu-icon-ios-arrow-down'
        }
        this[nameShow] = !this[nameShow]
      },
      /* 获取当前创建项目 */
      getProject(Id) {
        this.$post('ProjectInfo.findProjectByID', {
            Id: Id || this.$route.query.ProjectId
        }).then(res => {
          console.log(res)
          this.downloadInfo = res.doc
          res.info.PerjectLeader = res.PerjectLeader  // 项目负责人显示数字问题
          this.info = res.info
          // 项目名字留痕
          if(res.pt2List.length>0) {
            var arrAll = res.pt2List //所有更改记录
            function arrayUnique2(arr, ProjectName) {
              var hash = {};
              return arr.reduce(function (item, next) {
                hash[next[ProjectName]] ? '' : hash[next[ProjectName]] = true && item.push(next);
                return item;
              }, []);
            }
            var projectNameList = arrayUnique2(arrAll, "ProjectName")
            if(projectNameList[0].ProjectName==this.info.ProjectName) {
              projectNameList = arrayUnique2(arrAll, "ProjectName").slice(1)
            }
            projectNameList.forEach(e => {
              this.projectNameList.push({name: e.ProjectName,Operator: e.Operator,UpdateTime: e.UpdateTime})
            });

            var buildNameList = arrayUnique2(arrAll, "BuildUnit")
            if(buildNameList[0].BuildUnit==this.info.BuildUnit) {
              buildNameList = arrayUnique2(arrAll, "BuildUnit").slice(1)
            }
            buildNameList.forEach(e => {
              this.buildNameList.push({name: e.BuildUnit,Operator: e.Operator,UpdateTime: e.UpdateTime})
            });          
          }
          
          localStorage.projectType = this.info.PerjectType
          this.flag = true
          this.ReportBatch = this.upWord(res.doc.ReportBatch)
        }).catch(err => {
          console.log(err)
          this.$Message.error('获取当前项目失败')
          // alert('请求崩溃1231')
        })
      },
      findStaffs() {
        axios.get('/audit/findStaffs2', {
          params: {
            ProjectId: this.$route.query.ProjectId
          }
        }).then(res => {
          console.log(res)
          this.cyz = res.data
          // if (res._STATUS == 1) {
          //   let cyz = []
          //   res.data.forEach((e, i) => {
          //     cyz.push(e['Staffs_name'])
          //   });
          //   this.cyz = cyz.join(',')
          // }
        })
      },
      // 保存 save / 提交 submit
      submit(type) {
        let info = this.info
        info.BtnWay = type == 'save' ? 1 : 2
        info.PerjectType = type == 'save' ? 13 : 13

        if(type == 'save'){
          if(this.setButton == 0){
            this.setButton = 1
            setTimeout(()=>{this.setButton = 0},60000)
            axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
              if (res._STATUS == 1) {
                let title = res._MESSAGE
                if (type == 'save') {
                  this.$Modal.success({
                    title,
                  })
                } else {
                  this.$router.push({
                    name: "reportPublicity",
                    query: {
                      ProjectId: this.$route.query.ProjectId
                    }
                  })
                }
              } else {
                this.$Modal.error({title:res._MESSAGE})
              }
            }).catch(err => {
              this.$Message.error('请求失败!')
            })
          }else{
            this.$Message.warning('连续点击保存时间过短，最少间隔1分钟!')
          }
        }else{
          if(this.updatedInfo == 0){
            this.updatedInfo = 1
            setTimeout(()=>{this.updatedInfo = 0},60000)
            axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
              if (res._STATUS == 1) {
                let title = res._MESSAGE
                if (type == 'save') {
                  this.$Modal.success({
                    title,
                  })
                } else {
                  this.$router.push({
                    name: "reportPublicity",
                    query: {
                      ProjectId: this.$route.query.ProjectId
                    }
                  })
                }
              } else {
                this.$Modal.error({title:res._MESSAGE})
              }
            }).catch(err => {
              this.$Message.error('请求失败!')
            })
          }else{
            this.$Message.warning('连续点击提交时间过短，最少间隔1分钟!')
          }
        }


        // if(this.setButton == 0){
        //   this.setButton = 1
        //   setTimeout(()=>{this.setButton = 0},60000)
        
        // let info = this.info
        // info.btn_way = type == 'save' ? 1 : 2
        // info.Perject_type = type == 'save' ? 13 : 13

        // // if(this.updatedInfo <= 2){
        // //   this.$Message.warning('未修改数据无法提交')
        // //   return
        // // }
        // axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
        //   if (res._STATUS == 1) {
        //     let title = res._MESSAGE
        //     if (type == 'save') {
        //       this.$Modal.success({
        //         title,
        //       })
        //     } else {
        //       this.$router.push({
        //         name: "reportPublicity",
        //         query: {
        //           // id: this.$route.query.id || this.$route.query.Project_id
        //           Project_id: this.$route.query.Project_id
        //         }
        //       })
        //     }
        //   } else {
        //     // 提示
        //     this.$Modal.error({
        //       title:res._MESSAGE
        //     })
        //   }
        // }).catch(err => {
        //   console.log(err)
        //   this.$Message.error('请求失败!')
        // })
        // }else{
        //   this.$Message.warning('请勿重复提交!')
        // }
      },
      upWord(url){        //分割字符串
        if(url == null){
          return '上传'
        }else{
          var upWord = url;
          upWord = upWord.split("/");
          upWord = upWord.slice(-1);
          upWord = upWord.join('');
          // console.log(upWord)
          return upWord
        }
      },
    },
  }
</script>

<style lang="less">
  #reportApproval {
    td.sizeStyle {
      font-size:12px;
    }
    textarea.ivu-input {
      font-size: 12px;
    }
    .ivu-input {
      border: none !important;
      text-align: center;
      height: 30px;

      input {
        outline: medium;
      }
    }
    .ivu-input[disabled], fieldset[disabled] .ivu-input {
      background-color: #fff; 
      color: #333;
      font-size: 12px;
    }
  }
</style>